package chucnang;



import javax.swing.JPanel;



import trangchu.TrangChu;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;

import net.miginfocom.swing.MigLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.Timer;
import javax.swing.table.DefaultTableModel;

import form.DSJ_FormMuaHang;
import form.DSJ_KhoHang;

public class TonKho extends JPanel implements ActionListener{

	private JPanel plNorth,plCenter,plSouth;
	private JButton btnXem;
	public JComboBox cmbKhoHang;
	public JTable table;
	public DefaultTableModel model;
	String[][] data = {};
    String[] colunms = {"Mã Hàng","Tên Hàng","Kho Hàng","Số Lượng","Đơn Vị","Nhóm Hàng"};
	public TonKho(TrangChu trangchu) {
		setLayout(new BorderLayout(10, 10));
		
		plNorth = new JPanel();
		add(plNorth, BorderLayout.NORTH);
		plNorth.setLayout(new MigLayout("", "[][grow]", "[]"));
		
		btnXem = new JButton("Xem");
		plNorth.add(btnXem, "cell 0 0");
		
		cmbKhoHang = new JComboBox();
		cmbKhoHang.addItem("Tất Cả");
		plNorth.add(cmbKhoHang, "cell 1 0,alignx left");
		
		plCenter = new JPanel();
		add(plCenter,BorderLayout.CENTER);
		plCenter.setLayout(new BorderLayout());
    	JScrollPane scrollPane = new JScrollPane();
    	plCenter.add(scrollPane);
		
    	model = new DefaultTableModel(data, colunms);
		table = new JTable(model);
		table.getColumnModel().getColumn(0).setPreferredWidth(130);
		table.getColumnModel().getColumn(1).setPreferredWidth(400);
    	table.getColumnModel().getColumn(2).setPreferredWidth(200);
    	table.getColumnModel().getColumn(3).setPreferredWidth(100);
		table.getColumnModel().getColumn(4).setPreferredWidth(100);
    	table.getColumnModel().getColumn(5).setPreferredWidth(200);
		scrollPane.setViewportView(table);
		
		plSouth = new JPanel();	
		plSouth.setLayout(new BorderLayout());
		add(plSouth, BorderLayout.SOUTH);
		JPanel plSouth2 = new JPanel();
		plSouth.add(plSouth2,BorderLayout.SOUTH);		
		plSouth2.setLayout(new FlowLayout(FlowLayout.RIGHT));
		
		JLabel lblgoithieu = new JLabel("Phần mềm quản lý bán hàng ShopM                ");
		lblgoithieu.setFont(new Font("Tahoma", 0, 10));
		lblgoithieu.setFont(new Font("Tahoma", 3, 11));		
		lblgoithieu.setForeground(Color.blue);
		plSouth2.add(lblgoithieu);
		final JLabel lblgioserver = new JLabel();
		plSouth2.add(lblgioserver);
		 final SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss a | dd/MM/yyyy");
	        Timer time = new Timer(1000, new ActionListener()
	        {
	            public void actionPerformed(ActionEvent e)
	            {
	                Date d = new Date(System.currentTimeMillis());
	                lblgioserver.setText(sdf.format(d.getTime()));
	            }
	        });
	        time.start();
		
		//skien
		cmbKhoHang.addActionListener(this);
		btnXem.addActionListener(this);
		
		DbselectKhoHang();
		listxemtatca();
		
	}
	//kho
	public void DbselectKhoHang(){
		DSJ_KhoHang data = new DSJ_KhoHang();
		Vector<DSJ_KhoHang> getKho = data.getUsers();
		for(int i=0;i<getKho.size();i++){
			cmbKhoHang.addItem(getKho.get(i).getTenKho());
		}
	}
	//hanghoa
	public void listxem(){
		DSJ_FormMuaHang b = new DSJ_FormMuaHang();
		Vector list = b.getKhoHangHoa(this);
		Vector list2;
		model.getDataVector().removeAllElements();
		for (int i = 0; i < list.size(); i++) {
			list2 = (Vector) list.get(i);
			Object[] oPerson = {
					list2.get(0).toString(),
					list2.get(1).toString(),
					list2.get(2).toString(),
					list2.get(3).toString(),
					list2.get(4).toString(),
					list2.get(5).toString()};
			
			model.insertRow(0, oPerson);
			
		}
		model.fireTableDataChanged();
	}
	public void listxemtatca(){
		DSJ_FormMuaHang b = new DSJ_FormMuaHang();
		Vector list = b.getHangHoa();
		Vector list2;
		model.getDataVector().removeAllElements();
		for (int i = 0; i < list.size(); i++) {
			list2 = (Vector) list.get(i);
			Object[] oPerson = {
					list2.get(0).toString(),
					list2.get(1).toString(),
					list2.get(2).toString(),
					list2.get(3).toString(),
					list2.get(4).toString(),
					list2.get(5).toString()};
			
			model.insertRow(0, oPerson);
			
		}
		model.fireTableDataChanged();
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == cmbKhoHang){
			listxem();
			if(cmbKhoHang.getSelectedItem().toString().equals("Tất Cả")){
				listxemtatca();
			}
		}
		if(e.getSource() == btnXem){
			listxemtatca();
		}
		
	}

}
